<?php
namespace Human\Model;
use Think\Model;
/**
 * 职务操作
 */
class JobModel extends Model {

    //完成
    protected $_auto = array (

    );

    //验证
    protected $_validate = array(
        array('job_name','require', '请填写部门名称'),
        array('department_id','require', '请选择部门'),
        array('job_sort','require', '请填写部门排序'),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0){
        $data   = $this->table("__JOB__ as A")
                    ->join('__DEPARTMENT__ as B ON A.department_id = B.department_id')
                    ->field('A.*,B.department_name')
                    ->where($where)
                    ->limit($limit)
                    ->order('A.job_id')
                    ->select();
        return $data;
    }

    /**
     * 获取数量
     * @return int 数量
     */
    public function countList($where = array()){

        return   $this->table("__JOB__ as A")
                    ->join('__DEPARTMENT__ as B ON A.department_id = B.department_id')
                    ->field('A.*,B.department_name')
                    ->where($where)
                    ->count();
    }

    /**
     * 获取信息
     * @param int $jobId ID
     * @return array 信息
     */
    public function getInfo($jobId)
    {
    	return $this->where('job_id ='.$jobId)->find();
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where)
    {

        return $this->where($where)->find();

    }

    /**
     * 更新信息
     * @param string $type 更新类型
     * @return bool 更新状态
     */
    public function saveData($type = 'add'){
        
        //事务处理
        $this->startTrans();
        $data = $this->create();
        if(!$data){
            $this->rollback();
            return false;
        }
        if($type == 'add'){
            //写入数据
            $data['job_create_time']=time();
            $status = $this->add($data);
            if($status){
                $this->commit();
            }else{
                $this->rollback();
            }
            return $status;
        }
        if($type == 'edit'){
            //修改数据
            $status = $this->where('job_id='.$data['job_id'])->save();
            if($status === false){
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        }
        $this->rollback();
        return false;
    }

    /**
     * 删除信息
     * @param int $jobId ID
     * @return bool 删除状态
     */
    public function delData($jobId)
    {
        $map = array();
        $map['job_id'] = $jobId;
        return $this->where($map)->delete();
    }

    /**
     *获取某部门下的职位
     *@param int $departmentId ID
     *@return array 信息
     */

    public function getJobByDepartmentId($departmentId){
        $data = $this-> where(array('department_id'=>$departmentId))
                    -> field('job_id,job_name')
                    -> select();
        $html="<option value=''>--请选择职位--</option>";
        foreach ($data as $_k => $_v) {
            $html.="<option value='".$_v['job_id']."'>".$_v['job_name']."</option>";
        }
        return $html;
    }

}
